// pages/account/account.js

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
  },
  // 事件处理函数
  onLoad() {
    // 从本地缓存获取用户信息
    const userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      this.setData({
        userInfo: {
          avatarUrl: userInfo.user_img || '../../images/default_avatar.png',
          nickName: userInfo.user_name || '未设置昵称'
        },
        hasUserInfo: true
      });
    }

    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      });
    }
  },

  onShow() {
    this.loadUserInfoFromServer();
  },

  loadUserInfoFromServer() {
    const userId = wx.getStorageSync('userId');
    if (!userId) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      return;
    }
    wx.request({
      url: 'http://127.0.0.1:3001/getUserDetails',
      method: 'GET',
      data: { userId },
      success: (res) => {
        if (res.data && res.data.success && res.data.userDetails) {
          const userInfo = res.data.userDetails;
          this.setData({
            userInfo: {
              avatarUrl: userInfo.user_img || '../../images/default_avatar.png',
              nickName: userInfo.user_name 
            },
            hasUserInfo: true
          });
          wx.setStorageSync('userInfo', userInfo); // 同步更新本地缓存
          this.checkUserInfo(userInfo);
        } else {
          wx.showToast({
            title: res.data.message || '获取用户信息失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    });
  },

  // 检查用户信息是否完整
  checkUserInfo(userInfo) {
    let missingInfo = [];
    if (!userInfo.phone_number) {
      missingInfo.push('手机号');
    }
    if (!userInfo.sex) {
      missingInfo.push('性别');
    }

    if (missingInfo.length > 0) {
      wx.showModal({
        title: '提示',
        content: `请完善个人信息：${missingInfo.join('、')}`,
        showCancel: true,
        cancelText: '不了',
        confirmText: '去完善',
        success: (res) => {
          if (res.confirm) {
            const userId = wx.getStorageSync('userId');
            let url = '/pages/user_details/user_details';
            if (userId) {
              url += `?userId=${userId}`;
            }
            wx.navigateTo({
              url: url
            });
          }
        }
      });
    }
  },

  getUserProfile() {
    wx.getUserProfile({
      desc: '展示用户信息',
      success: (res) => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        });
      }
    });
  },
  getUserInfo(e) {
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    });
  },

  // 跳转到用户个人信息详情页
  goToUserDetails: function () {
    const userId = wx.getStorageSync('userId'); // 获取当前用户的ID，以便在详情页加载对应信息
    if (userId) {
      wx.navigateTo({
        url: `/pages/user_details/user_details?userId=${userId}`
      });
    } else {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
    }
  },
});